const express = require('express')
const router = express.Router()

module.exports = router

const db = require('../db/usersHandle')
const { formatData } = require('../utils')


//实现注册接口（POST /api/user/reg）
router.post('/reg', async (req, res) => {
    const { username, password,email } = req.body;
    console.log(username, password,email)
    const result = await db.reg(username, password,email)
    if (result) {
        res.send(formatData.success())
    } else {
        res.send(formatData.fail())
    }
})

//实现登录接口（POST /api/user/login）
router.post('/login',async (req,res)=>{
    const { username, password } = req.body;
    const result = await db.login(username, password)
    if(result[0]){
        res.send({
            code:200,
            data:result[0],
            msg:'success'
        })
    }else{
        res.send(formatData.fail())
    }
})


// 校验用户名是否被占用 ( GET /api/user/check)
router.get('/check', async (req, res) => {
    const { username } = req.query;
    const result = await db.checked(username)
    if (result) {
        res.send({
            code: 400,
            msg: 'user is exist',
        })
    } else {
        res.send(formatData.success())
    }
})

// 获取用户列表 ( GET /api/user/list)
router.get('/list', async (req, res) => {
    const username = req.query
    const result = await db.read()
    if (result) {
        res.send({
            data:result,
            code:200,
            msg:"成功"
        })
    } else {
        res.send(formatData.fail())
    }
})

// 获取单个用户信息 ( GET /api/user/:id)
router.get('/:id',async (req, res) => {
    let {userId } = req.query;
    const result = await db.query(userId)
    if (result) {
        // res.send(formatData.success())
        res.send({
                    data:result,
                    code:200,
                    msg:"成功"
                })
    } else {
        res.send(formatData.fail())
    }
})

// 新增用户 ( put /api/user/add)
router.put('/add',async (req, res) => {
    // const {username,password,email} = req.body
    const obj = req.body
    // const result = await db.create(username,password,email)
    // if (result) {
    //     res.send({
    //         data:result,
    //         code:200,
    //         msg:"成功"
    //     })
    // } else {
    //     res.send(formatData.fail())
    // }
    try {
        const result = await db.create(obj)
        res.send(formatData())

    } catch (err) {
        res.send(formatData.fail())
    }
})

// 修改用户信息 ( put /api/user/update/:id)   userId是必需
router.put('/update/:id',async (req, res) => {
    const obj = req.body
    try {
        const result = await db.update(obj)
        res.send(formatData())
    } catch (err) {
        res.send(formatData({ code: 400 }));
    }
})

// 删除某个用户 ( delete /api/user/delete/:id)
router.delete('/delete/:id',async (req, res) => {
    const { userId } = req.body;
    try {
        const result = await db.delete(userId)
        res.send(formatData())
    } catch (err) {
        res.send(formatData({ code: 400 }));
    }
})

// 批量删除 (delete /api/user/delete)  
// userIds为[]
router.delete('/delete',async (req, res) => {
    const { userIds } = req.body;
    try {
        const result = await db.remove(userIds)
        res.send(formatData())
    } catch (err) {
        res.send(formatData({ code: 400 }));
    }
})